﻿using Coldairarrow.Entity.Apk;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Apk
{
    public partial class Apk_EditionBusiness : BaseBusiness<Apk_Edition>, IApk_EditionBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public Apk_EditionBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<Apk_Edition>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<Apk_Edition>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<Apk_Edition, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<Apk_Edition> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(Apk_Edition data)
        {
            data.UploadDate = System.DateTime.Now;
            data.Deleted = false;
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(Apk_Edition data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        /// <summary>
        /// 获取PDA的apk最新版本
        /// </summary>
        /// <returns></returns>
        public async Task<Apk_Edition> GetApkEditionAsync()
        {
            Apk_Edition apkdata = await Db.GetIQueryable<Apk_Edition>().OrderByDescending(x => x.CreateTime).FirstOrDefaultAsync();
            return apkdata;
        }

        #endregion

        #region 私有成员

        #endregion
    }
}